package com.cmpe.av;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.cmpe.av.logic.UserLogic;
import com.cmpe.av.model.User;

/**
 * Servlet implementation class Register
 */
@WebServlet("/Register")
public class Register extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public Register() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		UserLogic msAccess;
		User user = new User();
		String name = request.getParameter("name");
		String surname = request.getParameter("surname");
		String userName = request.getParameter("username");
		String email = request.getParameter("email");
		String password = request.getParameter("password");
		String strErrMsg = null;
		HttpSession session = request.getSession();
		boolean nameExist = false;
		
		if(!checkEmail(email))
		{
			request.setAttribute("mailError", true);
			request.getRequestDispatcher("/default.jsp").forward(request,
					response);
			return;
		}
		
		try {
			msAccess = new UserLogic();
			nameExist = msAccess.isInDb(userName);
			if(nameExist)
			{
				request.setAttribute("userExists", true);
				request.getRequestDispatcher("/default.jsp").forward(request,
						response);
				return;
			}
			user.setName(name);
			user.setSurname(surname);
			user.setUsername(userName);
			user.setEmail(email);
			user.setPassword(password);
			user.setIsAdmin(0);
			msAccess.addUser(user);
			strErrMsg = "You are now registered. Please login.";
			request.setAttribute("validRegister", true);
			request.setAttribute("flag", true);
			request.setAttribute("registerMsg", strErrMsg);
			request.getRequestDispatcher("/default.jsp").forward(request,response);
		
			
		}catch (Exception e) {
			strErrMsg = "Unable to register user";
			request.setAttribute("flag", true);
			session.setAttribute("registerMsg", strErrMsg);
			System.out.println(e);
		}
		
	}
	
	public static boolean checkEmail(String email){
        int atPosition;
        atPosition=email.indexOf('@');
        if(atPosition==-1)
        	return false;
        int nt;
        nt=email.lastIndexOf('.');
        if(nt<atPosition)
                return false;
        if(nt==email.length()-1)
                return false;
        return true;
    }

}
